package com.appkubes.mall.admin.application.goods.category.controller;


import com.appkubes.cloud.config.params.paramValid.ParamValid;
import com.appkubes.common.Response;
import com.appkubes.common.utils.data.date.DateUtils;
import com.appkubes.mall.admin.application.goods.category.entity.CategorySearchVO;
import com.appkubes.mall.admin.application.goods.category.entity.CategoryVO;
import com.appkubes.mall.admin.application.goods.category.service.impl.GoodsCategoryServiceImpl;
import com.appkubes.mall.admin.config.camel.RequestModel;
import com.appkubes.mall.core.controller.BaseMallGoodsCategoryController;
import com.appkubes.mall.core.entity.MallGoodsCategory;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "分类模块")
@RestController
@RequestMapping("/sass_admin/Scategory")
public class GoodsCategoryController extends BaseMallGoodsCategoryController {

    @Autowired
    GoodsCategoryServiceImpl categoryService;

    @ApiOperation(value = "分类列表")
    @PostMapping("/getList")
    public Response getList(@ParamValid CategorySearchVO searchVO) {
        MallGoodsCategory query = new MallGoodsCategory();
        BeanUtils.copyProperties(searchVO, query);
        QueryWrapper<MallGoodsCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("sort");
        IPage<MallGoodsCategory> pageinfo = categoryService.selectPageByEntity(query, searchVO);
        return Response.success(this.getBaseMapper().selectPage(pageinfo, queryWrapper));
    }

    @ApiOperation(value = "添加分类")
    @PostMapping("/save")
    public Response save(@RequestModel CategoryVO categoryVO) {
        MallGoodsCategory entity = new MallGoodsCategory();
        BeanUtils.copyProperties(categoryVO, entity);
        entity.setUpdateTime(DateUtils.getCurrentTimestamp());
        entity.setAppKey(null);
        Boolean result = categoryService.saveOrUpdate(entity);
        return Response.success(result);
    }


    @ApiOperation(value = "删除分类")
    @PostMapping("/delete")
    public Response delete(String ids) {
        List<String> list = Arrays.asList(ids.split(","));
        Boolean result = categoryService.removeByIds(list);
        return Response.success(result);

    }
}
